<?php

namespace app\controller;

use app\BaseController;
use app\utils\Utils;
use think\facade\Request;
use think\facade\Cache;
use app\model\Log;


class Imgverify extends BaseController
{
    static public function make() // 生成图片
    {
        header('Content-Type: application/json');

        if (!Cache::get("ImgVerifyIP_" . Utils::getIp(), false)) {
        } elseif (time() - Cache::get("ImgVerifyIP_" . Utils::getIp(), false)['time'] < 15) { // 频繁
            $code = 403;
            $data = null;
            $msg = "请稍等15秒后重试";
            return json_encode(array('code' => $code,  'msg' => $msg));
        }
        Cache::set("ImgVerifyIP_" . Utils::getIp(), array('time' => time()), 10); // 设置单IP时间

        $extended_char = ""; // 特殊字符（没有留空即可）

        // 生成答案
        // 生成随机的5位数字和字母混合验证码
        $correct_code = '';
        for ($i = 0; $i < 5; $i++) {
            $correct_code .= Utils::random_char($extended_char);
        }

        $id = md5(time() . rand(0, 10000));
        $question = Utils::make_img($correct_code);

        $choose = array();

        // 生成正确答案图片
        $answer = Utils::make_img($correct_code);

        $choose[] = array('id' => md5($answer), 'base' => $answer); // 插入返回数据

        Cache::set($id, array('answer' => md5($answer), 'status' => false), 240); // 设置答案和数据

        // 生成剩余三个图片
        for ($a = 0; $a < 3; $a++) {
            $code = '';
            $times = 0;

            // 如果 $code 为空、长度不够或等于 $correct_code 时，则清空并生成一遍
            while ($code == '' || strlen($code) < 5 || $code == $correct_code) {
                $code = '';

                for ($i = 0; $i < 5; $i++) {
                    $rand = rand(1, 3);

                    // 有 三分之二 的概率 取 $correct_code 中对应的字符，且连续使用次数不能达到3次
                    if ($rand > 1 && $times < 3) {
                        $code .= $correct_code[$i];
                        $times++;
                    } else {
                        $code .= Utils::random_char($extended_char);
                        $times = 0;
                    }
                }
            }

            $choose_img = Utils::make_img($code);
            $choose[] = array('id' => md5($choose_img), 'base' => $choose_img); // 插入返回数据
        }

        shuffle($choose); // 打乱数组排列

        $code = 200;
        $data = array('question'  => $question, 'choose' => $choose, 'id' => $id);
        $msg = "获取成功";
        return json_encode(array('code' => $code, 'data' => $data, 'msg' => $msg));
    }

    static public function check() // 检查答案
    {
        header('Content-Type: application/json');

        $id = Request::post('id', false);
        if (!$id) { // 错误参数
            $code = 403;
            $msg = "参数有误";
            return json_encode(array('code' => $code, 'msg' => $msg));
        } else {
            $choose = Request::post('choose');
            $answer = Cache::get($id, false);
            if (!$answer) { // 不存在数据
                $code = 404;
                $msg = "验证失败，请重新验证";
                return json_encode(array('code' => $code,  'msg' => $msg));
            } else {
                if ($answer['answer'] == $choose) { // 选项ID对比成功
                    Cache::set($id, array('answer' => $answer['answer'], 'status' => true), 240); // 设置答案和数据
                    $code = 200;
                    $msg = "验证成功";
                    return json_encode(array('code' => $code, 'msg' => $msg));
                } else {
                    Cache::delete($id); // 删除缓存，防止爆破
                    $code = 403;
                    $msg = "验证失败，请重新验证";
                    return json_encode(array('code' => $code,  'msg' => $msg));
                }
            }
        }
    }
    static public function check_again()
    {
        header('Content-Type: application/json');

        $id = Request::post('id', false);
        if (!$id) {
            $code = 403;
            $msg = "参数有误";
            return json_encode(array('code' => $code, 'msg' => $msg));
        } else {
            $answer = Cache::get($id, false);
            if (!$answer) { // 不存在数据
                $code = 404;
                $msg = "验证失败";
                return json_encode(array('code' => $code, 'msg' => $msg));
            } else {
                if ($answer['status']) { // 已验证
                    $code = 200;
                    $msg = "验证成功";
                    return json_encode(array('code' => $code, 'msg' => $msg));
                } else {
                    Cache::delete($id); // 删除缓存，防止爆破
                    $code = 403;
                    $msg = "验证失败";
                    return json_encode(array('code' => $code, 'msg' => $msg));
                }
            }
        }
    }
}
